package com.zhong.gang.applib.base.act;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;

import com.zhong.gang.applib.utils.ActivityController;
import com.zhong.gang.applib.utils.CommonTitleBar;
import com.zhong.gang.applib.BaseApp;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.databinding.BaseLayoutBinding;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.rx.RxManager;

import okhttp3.Call;

public abstract class ActBase<T extends ViewDataBinding> extends FragmentActivity implements View.OnClickListener{
    private RxManager rxManager;
    public T bind;
    private BaseLayoutBinding basebind;
    private View view;
    protected ActBase mActivity;
    private TextView centerView;

    @Override
    protected void onResume() {
        super.onResume();
        if (!(getApplication() instanceof BaseApp)){
            closeActivity();
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity=this;
        basebind = DataBindingUtil.setContentView(this, R.layout.base_layout);
        basebind.setBaseact(this);
        view = LayoutInflater.from(this).inflate(LayoutRes(),null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        try {
            bind = DataBindingUtil.bind(view);
        }catch (Exception e){
        }
        basebind.mainlayout.addView(view);
        if (showLoading()){
            showLoadingFrgamnet();
        }else {
            showMainFragemt();
        }
        basebind.errorBtn.setOnClickListener(this);
        String titlestr = CreateTitle();
        if (titlestr != null)
            setTitle(titlestr);
        basebind.getRoot().setBackgroundColor(getResources().getColor(R.color.white));
        getTitleBar().setBackgroundColor(getResources().getColor(R.color.white));
        if (neeBack())
            setBackImg(R.mipmap.icon_back);
        initView();
        ActivityController.getInstance().addAct(this);
    }

    public CommonTitleBar getTitleBar(){
        return basebind.titlebar;
    }

    public void setTitle(String str){
        if (centerView == null){
            View centerView = LayoutInflater.from(this).inflate(R.layout.layout_titlebar_text,null);
            this.centerView = (TextView)centerView.findViewById(R.id.center_tv);
            this.centerView.setText(str);
            getTitleBar().setCenterView(centerView);
        }else {
            centerView.setText(str);
        }
    }

    public void setBackImg(int res){
        View leftView = LayoutInflater.from(this).inflate(R.layout.layout_back_img,null);
        ((ImageView)leftView.findViewById(R.id.center_tv)).setImageResource(res);
        getTitleBar().setLeftView(leftView);
        leftView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });
    }

    public View getRoot(){
        return basebind.baseLayout;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.error_btn) {
            showLoadingFrgamnet();
            getData();
        }
    }


    public RxManager getRxManager(){
        if (rxManager == null)
            rxManager = new RxManager();
        return rxManager;
    }

    /**
     * 跳转activity
     */
    public void openActivity(Class classes) {
        Intent intents = new Intent(this,classes);
        startActivity(intents);

    }
    /**
     * 跳转activity
     */
    public void openActivity(Intent intent) {
        startActivity(intent);
    }

    public void closeActivity(){
        if (!this.isFinishing())
            finish();
    }


    /**完全关闭APP*/
    public void CloseApp() {
        android.os.Process.killProcess(android.os.Process.myPid());    //获取PID
        System.exit(0);   //常规java、c#的标准退出法，返回值为0代表正常退出
    }

    /**
     * 显示加载Fragment
     */
    public void showLoadingFrgamnet(){
        basebind.errFragment.setVisibility(View.GONE);
        basebind.loadingFragment.setVisibility(View.VISIBLE);
    }

    /**
     * 显示加载出错Frament
     */
    public void showErrorFragment(){
        basebind.errFragment.setVisibility(View.VISIBLE);
        basebind.loadingFragment.setVisibility(View.GONE);
    }
    /**
     * 显示主布局
     */
    public void showMainFragemt(){
        basebind.errFragment.setVisibility(View.GONE);
        basebind.loadingFragment.setVisibility(View.GONE);
    }

    public View getErrView(){
        return basebind.errFragment;
    }

    public View getLoadingView(){
        return basebind.loadingFragment;
    }

    public View getContentView(){
        return view;
    }

    @LayoutRes
    public abstract int LayoutRes();
    @NonNull
    public abstract String CreateTitle();
    public boolean showLoading(){
        return false;
    }
    public void getData(){}
    public abstract void initView();
    protected boolean neeBack(){
        return true;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getRxManager().clear();
        ActivityController.getInstance().removeAct(this);
    }

    public class ActCallBack<T extends BaseDataBean> extends DataCallBack<T> {
        public ActCallBack() {
            super(ActBase.this);
        }

        @Override
        public void onResponse(T data) throws Exception {
            super.onResponse(data);
            showMainFragemt();
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            super.onError(call, e, id);
            showErrorFragment();
        }

        @Override
        public void onError(String str) {
            super.onError(str);
            showErrorFragment();
        }
    }




}
